package ru.st.selenium.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: gor
 * Date: 20.10.13
 * Time: 20:01
 * To change this template use File | Settings | File Templates.
 */
public class AddPage {

    public static void addMovieFillForm(WebDriver driver, String[] params){

        String iMDBNum, title, alsoKnownAs, year, duration, rating, format, trailerUrl, personalNotes, taglines,
                plotOutline, plots, languages, subs, audio, video, country, genre, director, writer, producer,
                music, cast;
        Boolean iOwn, iHaveSeen, loanedOut;
        //TODO еще нужно путь для файла обложки

        try{
            if (params.length != 26){
                System.out.println("Необходимо 26 параметров");  //+1 в туду
                throw new RuntimeException("Params are wrong!");
            }
            iMDBNum = params[0];
            title = params[1];
            alsoKnownAs = params[2];
            year = params[3];
            duration = params[4];
            rating = params[5];
            format = params[6];
            iOwn = Boolean.valueOf(params[7]);
            iHaveSeen = Boolean.valueOf(params[8]);
            loanedOut = Boolean.valueOf(params[9]);
            trailerUrl = params[10];
            personalNotes = params[11];
            taglines = params[12];
            plotOutline = params[13];
            plots = params[14];
            languages = params[15];
            subs = params[16];
            audio = params[17];
            video = params[18];
            country = params[19];
            genre = params[20];
            director = params[21];
            writer = params[22];
            producer = params[23];
            music = params[24];
            cast = params[24];


        }
        catch (Exception e){
            System.out.println(e);
            throw new RuntimeException("Params are wrong!");
        }

        driver.findElement(By.name("imdbid")).clear();
        driver.findElement(By.name("imdbid")).sendKeys(iMDBNum);

        driver.findElement(By.name("name")).clear();
        driver.findElement(By.name("name")).sendKeys(title);

        driver.findElement(By.name("aka")).clear();
        driver.findElement(By.name("aka")).sendKeys(alsoKnownAs);

        driver.findElement(By.name("year")).clear();
        driver.findElement(By.name("year")).sendKeys(year);

        driver.findElement(By.name("duration")).clear();
        driver.findElement(By.name("duration")).sendKeys(duration);

        driver.findElement(By.name("rating")).clear();
        driver.findElement(By.name("rating")).sendKeys(rating);

        driver.findElement(By.name("format")).clear();
        driver.findElement(By.name("format")).sendKeys(format);
        //
        if (iOwn){
            driver.findElement(By.id("own_yes")).click();
        }
        else {
            driver.findElement(By.id("own_no")).click();
        }
        //
        if (iHaveSeen){
            driver.findElement(By.id("seen_yes")).click();
        }
        else {
            driver.findElement(By.id("seen_no")).click();
        }
        //
        if (loanedOut){
            driver.findElement(By.id("loaned_yes")).click();
            //TODO - заполнение еще 2х открывшихся полей
        }
        else {
            driver.findElement(By.id("loaned_no")).click();
        }


        driver.findElement(By.name("trailer")).clear();
        driver.findElement(By.name("trailer")).sendKeys(trailerUrl);

        driver.findElement(By.name("notes")).clear();
        driver.findElement(By.name("notes")).sendKeys(personalNotes);

        driver.findElement(By.name("taglines")).clear();
        driver.findElement(By.name("taglines")).sendKeys(taglines);

        driver.findElement(By.name("plotoutline")).clear();
        driver.findElement(By.name("plotoutline")).sendKeys(plotOutline);

        driver.findElement(By.name("plots")).clear();
        driver.findElement(By.name("plots")).sendKeys(plots);

        driver.findElement(By.name("languages")).clear();
        driver.findElement(By.name("languages")).sendKeys(languages);

        driver.findElement(By.name("subtitles")).clear();
        driver.findElement(By.name("subtitles")).sendKeys(subs);

        driver.findElement(By.name("audio")).clear();
        driver.findElement(By.name("audio")).sendKeys(audio);

        driver.findElement(By.name("video")).clear();
        driver.findElement(By.name("video")).sendKeys(video);

        driver.findElement(By.name("country")).clear();
        driver.findElement(By.name("country")).sendKeys(country);

        driver.findElement(By.name("genres")).clear();
        driver.findElement(By.name("genres")).sendKeys(genre);

        driver.findElement(By.name("director")).clear();
        driver.findElement(By.name("director")).sendKeys(director);

        driver.findElement(By.name("writer")).clear();
        driver.findElement(By.name("writer")).sendKeys(writer);

        driver.findElement(By.name("producer")).clear();
        driver.findElement(By.name("producer")).sendKeys(producer);

        driver.findElement(By.name("music")).clear();
        driver.findElement(By.name("music")).sendKeys(music);

        driver.findElement(By.name("cast")).clear();
        driver.findElement(By.name("cast")).sendKeys(cast);

    }

    public static void addMovieFromIMDB(){

    }

    public static void clickSaveOnTop(WebDriver driver){
        driver.findElement(By.xpath("//img[@title='Save']")).click();
    }

    public static void clickSaveOnBottom(WebDriver driver){
        driver.findElement(By.id("submit")).click();
    }

    public static HashMap<String, String> getErrors(WebDriver driver){
        //вернем 2 возможные ошибки
        //TODO это не все ошибки
        HashMap<String, String> result = new HashMap<String, String>();

        String error;
        error = driver.findElement(By.xpath("//input[@name='name']/../label")).getText();
        result.put("name", error);
        System.out.println("DEBUG - name error added");
        error = driver.findElement(By.xpath("//input[@name='year']/../label")).getText();
        result.put("year", error);
        System.out.println("DEBUG - year error added");
        return result;
    }

}
